package ca.etsmtl.log430.lab2;

/**
 * This class presents the user with menus, accepts their choice, ensures their
 * choice is valid, and returns their choice to the caller. The menu is
 * presented as follows:
 * <pre>
 *    1) List Students
 *    2) List Courses
 *    3) List Courses a Student has Registered for
 *    4) List Students Registered for a Course
 *    5) Register a Student for a Course
 *    X) Exit.</pre>
 * 
 * @author A.J. Lattanze, CMU
 * @version 1.2, 2011-Feb-02.
 */

/*
 * Modification Log
 * ***************************************************************************
 * v1.2, 2011-Feb-02, R. Champagne - Various refactorings, javadoc comments.
 * 
 * v1.1, 2002-May-21, R. Champagne - Adapted for use at ETS.
 * 
 * v1.0, 12/29/99, A.J. Lattanze - Original version.
 * ***************************************************************************
 */

public class Menus {

	public char mainMenu() {

		Termio Terminal = new Termio();
		char UserChoice = ' ';
		boolean Error = true;

		while (Error) {

			System.out.println("\n\n1) List Students");
			System.out.println("2) List Courses");
			System.out.println("3) List Courses a Student has Registered for");
			System.out.println("4) List Students Registered for a Course");
			System.out.println("5) Register a Student for a Course");
			System.out.println("6) Register a Student for a Course (AUTO ASSIGN GROUP) (modif5)");
			System.out.println("7) List courses a student already succeed (modif1)");
			System.out.println("8) List mandatory courses not yet completed for a student (modif2)");
			System.out.println("X) Exit");
			System.out.print("\n\nEnter your choice and press return >> ");

			UserChoice = Terminal.keyboardReadChar();

			if ((UserChoice != 'X') && (UserChoice != 'x')
					&& (UserChoice < '1') && (UserChoice != '2')
					&& (UserChoice != '3') && (UserChoice < '4')
					&& (UserChoice != '5')
					&& (UserChoice != '6')
					&& (UserChoice != '7')
					&& (UserChoice != '8')
			) {

				System.out.print("\n\n*** Invalid Choice:: " + UserChoice
						+ " ***");

			} else {

				Error = false;

			} // if

		} // while

		return (UserChoice);

	} // MainMenu

	public Student pickStudent(StudentList ListObject) {

		Termio Terminal = new Termio();
		String UserChoice;
		Student StudentObject = null;

		System.out.print("\n\nEnter Student ID and press return >> ");
		UserChoice = Terminal.keyboardReadString();

		StudentObject = ListObject.findStudentByID(UserChoice);

		if (StudentObject == null) {

			System.out.println("\n\n*** Student ID " + UserChoice
					+ " not found ***");

		} // if

		return (StudentObject);

	} // PickStudent

	public Course pickCourse(CourseList ListObject) {

		Termio Terminal = new Termio();
		String UserChoiceCourseID;
		String UserChoiceSection;
		Course CourseObject = null;

		System.out.print("\n\nEnter Course ID and press return >> ");
		UserChoiceCourseID = Terminal.keyboardReadString();

		System.out.print("\n\nEnter Course Section and press return >> ");
		UserChoiceSection = Terminal.keyboardReadString();

		CourseObject = ListObject.findCourseByIDAndSection(UserChoiceCourseID,
				UserChoiceSection);

		if (CourseObject == null) {
			System.out.print("\n\n*** Course ID:" + UserChoiceCourseID);
			System.out.println(" Section:" + UserChoiceSection
					+ " not found ***");

		} // if

		return (CourseObject);

	} // PickCourse
	
	public Course autoPickCourse(CourseList ListObject) {

		Termio Terminal = new Termio();
		String UserChoiceCourseID;
		String UserChoiceSection;
		Course CourseObject = null;

		System.out.print("\n\nEnter Course ID and press return >> ");
		UserChoiceCourseID = Terminal.keyboardReadString();

		boolean flag = true;
		String group = "";
		int groupid = 1;
		
		while(flag) {
			
			group = String.valueOf(groupid);			
			CourseObject = ListObject.findCourseByIDAndSection(UserChoiceCourseID, group);			
			String startTime = CourseObject.getStartTime();
						
			if(groupid == 10 || CourseObject != null)
				flag = false ;
			
			boolean flagConflit = false;
			
			for(Object coursInscript : ListObject.getItemList()){
				
				Course unCoursInscript = (Course) coursInscript;
				
				if(unCoursInscript.getStartTime().equalsIgnoreCase(startTime)){
					flagConflit = true;
				}
			}
			
			if(!flagConflit){
				return (CourseObject);
			}	
			
			groupid++;
			group = String.valueOf(groupid);
			
		}//end while
		
		//all group cause conflicts
		System.out.println("Tous les groupes de ce cours cree un conflit");
		return null;

	} // PickCourse

} // Menus
